<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="classification">
        <el-input
          v-model="queryParams.classification"
          placeholder="请输入分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库" prop="warehouse">
        <el-select v-model="queryParams.warehouseId" placeholder="请选择仓库">
          <el-option
            v-for="dict in warehouseListS"
            :key="dict.warehouseName"
            :label="dict.warehouseName"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAddInventory"
          v-hasPermi="['business:inventory:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleWareHouseInfo"
          v-hasPermi="['business:inventory:add']"
        >仓库管理</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:inventory:add']"
        >分类管理</el-button>
      </el-col>


      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inventoryList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" prop="id"  />
      <el-table-column label="物品" align="center" prop="png"  >
        <template slot-scope="scope">
          <img :src="scope.row.png" width="100px" />
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="品牌" align="center" prop="brand"/>
      <el-table-column label="分类" align="center" prop="classification" />
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:inventory:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:inventory:remove']"
            v-if="scope.row.quantity == 0"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleOPIPOfwarehouse(scope.row)"
            v-hasPermi="['business:inventory:edit']"
          >出入库明细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" type="number" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="分类" prop="classification">
          <el-input v-model="form.classification" placeholder="请输入分类" />
        </el-form-item>

        <el-form-item label="仓库" prop="warehouseId" v-if="form.id == null">
          <el-select v-model="form.warehouseId" placeholder="请选择仓库">
            <el-option
              v-for="dict in warehouseListS"
              :key="dict.warehouseName"
              :label="dict.warehouseName"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="图片" prop="model" >
          <el-upload
            ref="upfile"
            action="upload"
            style="display: inline"
            :auto-upload="false"
            :on-change="handleChange"
            :file-list="fileList"
            :limit="1"
            list-type="picture"
            :on-remove="handleDeleteFire"
          >
            <el-button type="success">选择文件</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description"  placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--仓库表-->
    <el-dialog :title="title" :visible.sync="warehouseOpen" width="80%" append-to-body>
      <template>
        <div class="app-container">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="关键字" prop="key">
              <el-input
                v-model="queryParams.key"
                placeholder="请输入仓库名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryCK">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQueryCK">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['business:warehouse:add']"
              >添加仓库</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="warehouseList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" align="center" prop="id" />
            <el-table-column label="仓库名称" align="center" prop="warehouseName" />
            <el-table-column label="仓库地址" align="center" prop="warehouseAddress" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdateCK(scope.row)"
                  v-hasPermi="['business:warehouse:edit']"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDeleteCK(scope.row)"
                  v-hasPermi="['business:warehouse:remove']"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />

          <!-- 添加或修改仓库信息对话框 -->
          <el-dialog :title="title" :visible.sync="warehouseAddOpen" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
              <el-form-item label="仓库名称" prop="warehouseName">
                <el-input v-model="form.warehouseName"  placeholder="请输入仓库名称" />
              </el-form-item>
              <el-form-item label="仓库地址" prop="warehouseAddress">
                <el-input v-model="form.warehouseAddress" placeholder="请输入仓库地址" />
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitFormWarehouse">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
            </div>
          </el-dialog>
        </div>
      </template>
    </el-dialog>

    <!--明细表-->
    <el-dialog :title="title" :visible.sync="particularsOpen" width="50%" append-to-body>
      <el-table v-loading="loading" :data="particularsList" >
        <el-table-column label="序号" align="center" prop="id"  />
        <el-table-column label="类型" align="center" prop="type"  >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.store_put_out" :value="scope.row.type"/>
          </template>
        </el-table-column>
        <el-table-column label="操作时间" align="center" prop="operationTime" />
        <el-table-column label="物品" align="center" prop="goods" />
        <el-table-column label="品牌" align="center" prop="brand" />
        <el-table-column label="仓库" align="center" prop="warehouseName" />
        <el-table-column label="数量" align="center" prop="quantity" />
        <el-table-column label="单价" align="center" prop="unitPrice" />
      </el-table>
    </el-dialog>



  </div>
</template>

<script>
  import { listInventory, getInventory, delInventory, addInventory, updateInventory,OPIPOfwarehouse } from "@/api/business/inventory";
  import { listWarehouse, getWarehouse, delWarehouse, addWarehouse, updateWarehouse } from "@/api/business/warehouse";


  export default {
    dicts: ['store_put_out'],
    name: "Inventory",
    data() {
      return {

        //图片文件
        fileList :[],

        //高级查询仓库下拉表数据
        warehouseListS:[],
        //仓库弹窗
        warehouseOpen : false,
        //仓库数据
        warehouseList:[],
        //仓库新增弹窗
        warehouseAddOpen : false,
        //明细弹窗
        particularsOpen : false,
        //明细表参数
        particularsList : [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 库存管理表格数据
        inventoryList: [

        ],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          classification: null,
          warehouseId:null,
        },


        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name:[
            {required: true, message: "名称不能为空",trigger:"blur"}
          ],
          classification:[
            {required: true, message: "分类不能为空",trigger:"blur"},
          ],
          model:[
            {required: true, message: "必须上传图片",trigger:"submit"}
          ],
          quantity:[
            {required: true, message: "数量不能为空",trigger:"submit"}
          ]
        }
      };
    },
    created() {
      this.getList();
      this.getWarehouseList();
    },
    methods: {
      /** 查询库存管理列表 */
      getList() {
        this.fileList = []
        this.loading = true;
        listInventory(this.queryParams).then(response => {
          this.inventoryList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.warehouseAddOpen = false;
        this.fileList = [];
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          model: null,
          name: null,
          brand: null,
          classification: null,
          quantity: null,
          description: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.queryParams.warehouseId = null;
        this.handleQuery();
        this.fileList = []
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAddInventory() {
        this.reset();
        this.open = true;
        this.title = "添加库存管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {

        this.reset();
        const id = row.id || this.ids
        getInventory(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改库存管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
            if(this.form.name == null || this.form.name == ''){
              return;
            }
            if(this.form.quantity == null || this.form.quantity == ''){
              return;
            }
            if(this.form.classification == null || this.form.classification == ''){
              return;
            }
            let formDate = new FormData();
            formDate.append("name", this.form.name)
            formDate.append("brand", this.form.brand)
            formDate.append("quantity", this.form.quantity)
            formDate.append("classification", this.form.classification)
            formDate.append("description", this.form.description)
            formDate.append("warehouseId", this.form.warehouseId)

            if (valid) {
              if (this.form.id != null) {
                if(this.fileList[0] != null){
                  formDate.append("file", this.fileList[0].raw)
                }
                formDate.append("id", this.form.id)
                updateInventory(formDate).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                });
              } else {
                formDate.append("file", this.fileList[0].raw)
                addInventory(formDate).then(response => {
                  this.$modal.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                });
              }
            }
          }
        );
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除库存管理编号为"' + ids + '"的数据项？').then(function () {
          return delInventory(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('business/inventory/export', {
          ...this.queryParams
        }, `inventory_${new Date().getTime()}.xlsx`)
      },
      //新增图片表
      handleChange(file, fileList) {
        this.fileList = fileList;
        this.form.model = "2";
      },
      //图片删除表
      handleDeleteFire(file,fileList){
        this.fileList = [];
        if(this.form.id == null){
          this.form.model = null;
        }
      },

      //物品明细表
      handleOPIPOfwarehouse(row) {
        OPIPOfwarehouse(row.id).then((response) => {
          this.particularsList = response.data;
          this.particularsOpen = true;
        })
      },


      //仓库界面
      handleWareHouseInfo() {
        this.loading = true;
        listWarehouse(this.queryParams).then(response => {
          this.warehouseList = response.rows;
          this.total = response.total;
          this.loading = false;
          this.warehouseOpen = true;
        });
      },
      //仓库新增弹窗
      handleAdd(row) {
        this.reset();
        this.warehouseAddOpen = true;
        this.title = "添加仓库信息";
      },
      //仓库新增
      submitFormWarehouse() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateWarehouse(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.warehouseAddOpen = false;
                this.handleWareHouseInfo();
              });
            } else {
              addWarehouse(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.warehouseAddOpen = false;
                this.handleWareHouseInfo();
              });
            }
          }})
      },
      //仓库修改
      handleUpdateCK(row) {
        this.reset();
        const id = row.id || this.ids
        getWarehouse(id).then(response => {
          this.form = response.data;
          this.warehouseAddOpen = true;
          this.title = "修改仓库信息";
        });
      },
      //仓库删除
      handleDeleteCK(row) {
        const id = row.id;
        this.$modal.confirm('是否确认删除仓库信息编号为"' + id + '"的数据项？').then(function () {
          return delWarehouse(id);
        }).then(() => {
          this.handleWareHouseInfo();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      //仓库高级查询
      handleQueryCK() {
        this.queryParams.pageNum = 1;
        this.handleWareHouseInfo();
      },
      //仓库重置
      resetQueryCK() {
        this.resetForm("queryForm");
        this.handleQueryCK();
      },

      //高级查询 仓库列表
      getWarehouseList() {
        listWarehouse(this.queryParams).then(response => {
          this.warehouseListS = response.rows;
        });
      }
    }
  };

</script>

